package com.csw.shuanfa.CodeImprove.Transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * * //编程式事务之手动提交
 * * 查找死锁和主动杀死方法
 * * SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX;
 * * kill trx_mysql_thread_id;
 * 推荐自动提交
 */
@RestController
@RequestMapping("Transation")
public class PersonTransation {

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @RequestMapping("show")
    public void show() {
        TransactionStatus transactionStatus = platformTransactionManager.getTransaction(new DefaultTransactionDefinition());

        try {
            System.out.println(">>>>>>数据库操作");
        } catch (Exception e) {//放开异常范围
            platformTransactionManager.rollback(transactionStatus);
            throw e;
        }
        //全部成功在提交
        platformTransactionManager.commit(transactionStatus);
    }
}


